using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SO
{
    [CreateAssetMenu(fileName = "BackpackInventoryS0", menuName = "Data/BackpackInventoryS0")]
    public class BackpackInventoryS0 : ScriptableObject
    {
        public List<ItemInventoryData> itemList = new List<ItemInventoryData>();
        public ItemCellData AddItem(int id)
        {
            for (int i = 0; i < itemList.Count; ++i)
            {
                if (itemList[i].amount == 0) continue;

                if (itemList[i].id == id)
                {
                    itemList[i].amount += 1;
                    return new ItemCellData(itemList[i], i);
                }
            }
            for (int i = 0; i < itemList.Count; ++i)
            {
                if (itemList[i].amount == 0)
                {
                    itemList[i].id = id;
                    itemList[i].amount = 1;
                    return new ItemCellData(itemList[i], i);

                }
            }
            return null;
        }
        public ItemCellData RemoveItem(int id)
        {
            for (int i = 0; i < itemList.Count; ++i)
            {
                if (itemList[i].amount == 0) continue;

                if (itemList[i].id == id)
                {
                    itemList[i].amount -= 1;
                    if (itemList[i].amount == 0)
                    {
                        itemList[i].id = 0;
                    }
                    return new ItemCellData(itemList[i], i);
                }
            }
            return null;
        }
        public bool IsNull()
        {
            for (int i = 0; i < itemList.Count; ++i)
            {
                if (!(itemList[i].amount == 0))
                {
                    return false;
                }
            }
            return true;
        }

    }

    [System.Serializable]
    public class ItemInventoryData
    {
        public int id;
        public int amount;
        public ItemInventoryData(int id, int amount)
        {
            this.id = id;
            this.amount = amount;
        }
    }
    public class ItemCellData
    {
        public ItemInventoryData itemInventoryData;
        public int index;
        public ItemCellData(ItemInventoryData itemInventoryData, int index)
        {
            this.itemInventoryData = itemInventoryData;
            this.index = index;
        }
    }
}